package com.fangrui.controller;

import com.fangrui.anno.Log;
import com.fangrui.pojo.Emp;
import com.fangrui.pojo.PageBean;
import com.fangrui.pojo.Result;
import com.fangrui.service.EmpService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

//
// Created by ✎﹏ℳ๓独赏☆独醉 on 2025/4/22 18:16.
//
@Slf4j
@RestController
@RequestMapping("emps")
public class EmpController {
    @Autowired
    private EmpService empService;
//    @GetMapping
//    public Result getEmpPages(@RequestParam(name="page",required = false ) Integer pageNum, Integer pageSize){//此处可以通过defaultValue = "1"设置默认值
//        if(pageNum == null || pageSize == null){
//            pageNum = 1;
//            pageSize = 10;
//        }
//        if(pageNum < 1 || pageSize < 1){
//            return Result.error("页码或页面大小异常");
//        }
//        log.info("分页查询");
//        long total = empService.getTotal();
//        List<Emp> emps = empService.getAllEmps(pageNum,pageSize);
//        return Result.success(new PageBean(total,emps));
//    }
    @GetMapping
    public Result getEmpPages(String name, Short gender,
                              @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                              @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate end,
                              @RequestParam(name = "page", defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize){
        if(pageNum < 1 || pageSize < 1){
            return Result.error("页码或页面大小异常");
        }
        log.info("条件分页查询{},{},{},{},{},{}:",name,gender,begin,end,pageNum,pageSize);
        PageHelper.startPage(pageNum,pageSize);
        List<Emp> list = empService.getAllEmps(name,gender,begin,end);
        Page<Emp> page = (Page<Emp>) list;
        return Result.success(new PageBean(page.getTotal(),page.getResult()));
    }
    @Log
    @DeleteMapping("/{ids}")
    public Result deleteEmps(@PathVariable List<Integer> ids){
        if (ids == null || ids.size() == 0){
            return Result.error("没有要删除的记录");
        }
        int res = empService.deleteEmps(ids);
        if (res > 0){
            return Result.success();
        }else{
            return Result.error("未查找到相关记录");
        }
    }
    @Log
    @PostMapping
    public Result addEmp(@RequestBody Emp emp){
        log.info("新增员工：{}",emp);
        if (emp == null){
            return Result.error("新增用户失败");
        }
        int res = empService.addEmp(emp);
        if (res > 0){
            return Result.success();
        }else{
            return Result.error("新增用户失败");
        }
    }
    @GetMapping("/{id}")
    public Result getEmp(@PathVariable Integer id){
        log.info("根据id查询：{}",id);
        if (id == null || id < 0){
            return Result.error("id不合规");
        }
        Emp emp = empService.getEmpById(id);
        if (emp == null){
            return Result.error("未查询寻到用户");
        }
        return Result.success(emp);
    }
    @Log
    @PutMapping
    public Result updateEmp(@RequestBody Emp emp){
        if (emp == null){
            return Result.error("更新信息失败");
        }
        int res = empService.updateEmp(emp);
        if (res == 0){
            return Result.error("更新信息失败");
        }
        return Result.success();
    }
}
